perf(core): cache generated JSON schema per Class/Type to cut JsonSchemaUtils lock contention - #2
Draft
java-dependency-upgrade-fixer[bot] wants to merge 1 commit into
Conversation
…emaUtils lock contention
Owner
|
Campaign acceptance #2 (independently reviewed)
Accepted as a genuine, reviewable performance improvement; remains draft and unmerged. |
This was referenced Aug 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The base PR (mirroring agentscope-ai#2796) fixed a real thread-safety bug by serializing every call to the shared victools
SchemaGeneratorthrough a singleSCHEMA_LOCK, since the generator's JacksonModule keeps an unsynchronized introspection cache. That fix is correct but serializes every call, even repeat calls for the same class/type that the hot paths inReActAgent.doStructuredCallandToolSchemaGeneratorissue on every agent call / tool registration.This change caches the generated
JsonNodeperClass/Typein aConcurrentHashMap, soSCHEMA_LOCK(and the expensive reflectiveschemaGenerator.generateSchema(...)call it guards) is only needed the first time a given class or type is seen. Every call still performs its ownJsonUtilsconversion from the cached node to a brand-new, independently mutableMap, so callers that mutate the returned map (e.g.ToolSchemaGeneratoradding adescriptionkey) do not see or cause cross-call contamination.Why it's safe
Class/Type, static generator config) pair for the process lifetime, so caching by that key cannot become stale.JsonNodeitself is never mutated or exposed outside this class; only a freshMapcopy is ever returned, preserving the exact byte-for-byte schema content and the existing independent-mutability contract relied upon byToolSchemaGenerator.SCHEMA_LOCK, so the generator's internal introspection cache is still never touched concurrently — thread safety is unchanged.NullPointerExceptionraised for a null class/type argument, verified by new characterization tests.ConcurrentHashMapcannot grow without bound.Evidence
ToolSchemaGeneratorTest,ReActAgentStructuredOutputTest,ReActAgentStructuredOutputWithToolsTest) against the candidate; all pass.mvn -pl agentscope-core -am verifypasses on the final candidate.Limitations
Generated by JAIPilot Cloud for #1 from Anthropic session
sesn_0186Z4xnoTvr7tDTG1hkrVzf.